use std::io;
// Rng 是一个trait，定义了随机数生成器的实现
use rand::Rng;
use std::{thread, time};
use std::cmp::Ordering;


fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 100);
    // println!("secret_number: {}", secret_number);

   loop{

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin().read_line(&mut guess)
        .expect("Failed to read line");

    // 类型转换
    // 允许一个新变量来隐藏之前的值，用于类型转换
    // let guess: u32 = guess.trim().parse().expect("Please type a number");
    let guess: u32 = match guess.trim().parse(){
        Ok(num)=> num,
        Err(_)=>{// 出错时跳过 重新输入
            println!("你输入了错误的符号: {}, 请重新输入!", guess.trim());
            continue;
        }
    };

    println!("You guessed: {}",guess );
    match guess.cmp(&secret_number){
        Ordering::Less => println!("Too small"),
        Ordering::Greater => println!("Too big!"),
        Ordering::Equal => {
            println!("You win!");
            let second = 3;
            let duration = time::Duration::from_secs(second);
            println!("程序将在{}秒后退出.", second);
            thread::sleep(duration);
            break;
        }
    }

   }


}
